#include "DiyMenuForm.h"
#include <QMouseEvent>
DiyMenuForm::DiyMenuForm(ElementForm* parent):MainMenuForm(parent,5,72,5)
{

}



void DiyMenuForm::longPress()
{
	if (!_elements.empty()) {
		_elements.data()->longPress();
	}
}

void DiyMenuForm::setElements(QList<UIElement*> elements)
{
	ElementForm::setElements(elements);
	for (int i = 0; i < (_displayNum - 1) / 2; i++) {
		right();
	}
}

void DiyMenuForm::paintEvent(QPaintEvent* event)
{

	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing, true);

	Node<UIElement>* head = _elements.currentNode();
	Node<UIElement>* tail = _elements.currentNode();
	for (int i = 0; i < (_displayNum - 1) / 2; i++) {
		head = head->prev;
		tail = tail->next;
	}
	Node<UIElement>* temp = head;
	while (temp != tail) {
		temp = temp->prev;
		if (temp->data != nullptr) {
			temp->data->setRect(QRect(0, 0, 0, 0));
		}
	}

	for (int i = 0; i < _displayNum; i++)
	{
		head = head->next;
		if (head->prev->data != nullptr) {
			if (i < (_displayNum - 1)/2) {
				QRect rect = QRect(_leftDistance + i *(88), _topDistance,82 , 82);
				head->prev->data->drawElement(this, &painter, i == (_displayNum - 1) / 2, rect);
			}
			if(i==(_displayNum - 1)/2){
				QRect rect = QRect(_leftDistance + i * (88), _topDistance, 104, 104);
				head->prev->data->drawElement(this, &painter, i == (_displayNum - 1) / 2, rect);
			}
			if (i >(_displayNum - 1) / 2) {
				QRect rect = QRect(_leftDistance + (i-1) * (88)+110, _topDistance, 82, 82);
				head->prev->data->drawElement(this, &painter, i == (_displayNum - 1) / 2, rect);
			}
		}
	}

	TransparentForm::paintEvent(event);
}



void DiyMenuForm::mouseMoveEvent(QMouseEvent* event)
{
	if (_isPressed) {
		_endPos = event->pos();
		int offset = _endPos.x() - _startPos.x();
		if (qAbs(offset) > 50) {
			_isMoveFlag = true;
			_startPos = _endPos;
			if (offset > 50) {
				left();
			}
			else if (offset < -50) {
				right();
			}
		}
	}
	QWidget::mouseMoveEvent(event);
}

